//
//  LCGlobalLifeShareView.m
//  lechuang
//
//  Created by Mr_hi on 2024/8/30.
//  Copyright © 2024 Mooyi. All rights reserved.
//

#import "LCGlobalLifeShareView.h"


@interface LCGlobalLifeShareView ()

@property (weak, nonatomic) IBOutlet UIView *alertView;

@property (weak, nonatomic) IBOutlet UILabel *keyLb;

@property (assign, nonatomic) CGFloat alertW;

@property (assign, nonatomic) CGFloat alertH;

@property (copy, nonatomic)NSString *comandkey;

@end


@implementation LCGlobalLifeShareView

-(instancetype)initForNibWithKey:(NSString *)key {
    self = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:nil options:nil][0];
    self.comandkey = key;
    [self configUI];
    return self;
}

-(void)configUI {
    
    self.frame=CGRectMake(0, 0, ScreenW, ScreenH);
    self.alpha = 0;
    
    self.keyLb.text = self.comandkey;
    
    self.alertW = ScreenW-64;
    
    CGFloat keyLbH = [LCFunction lc_stringSizeWithFont:[UIFont systemFontOfSize:12] str:self.comandkey maxWidth:self.alertW-28-28 maxHeight:CGFLOAT_MAX].height;
    self.alertH = 56 + 14 + ceil(keyLbH) + 14 + 47 + 50 + 28;
    
    self.alertView.transform = CGAffineTransformMakeScale(1.0/self.alertW, 1.0/self.alertH);
    
    [self whenTapped:^{
        [self dismiss];
    }];
    [self setNeedsLayout];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
}

-(void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.25f animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(1, 1);
        self.alpha = 1;
        [self layoutIfNeeded];
    }];
}

-(void)dismiss {
    [UIView animateWithDuration:0.25f animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(1.0/self.alertW, 1.0/self.alertW);
        self.alpha = 1;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (IBAction)cancelBtnAction:(UIButton *)sender {
    [self dismiss];
}

- (IBAction)keyFuzhiAction:(UIButton *)sender {
    UIPasteboard *pab = [UIPasteboard generalPasteboard];
    [pab setString:self.comandkey];
    if(pab == nil) {
        [self showErrorText:@"复制失败"];
    }else{
        [self showSuccessText:@"复制成功"];
        [self dismiss];
    }
}


@end
